from win10toast import ToastNotifier
import requests
import os

if __name__ == '__main__':

    # 个人的URL
    sign_parameter = '自己登录的URL'
    # 以下4个变量，需要根据自己的情况进行修改，
    login_IP = 'http://10.23.2.4'
    not_sign_in_title = 'http://123.123.123.123'
    result_return = '"result":"success"'
    signed_in_title = 'success'

    # 以下4个变量，可根据自己的需要，决定是否修改
    already_icon = 'ico/Check.ico'
    success_icon = 'ico/Check.ico'
    false_icon = 'ico/Cross.ico'
    unknown_icon = 'ico/Questionmark.ico'

    try:
        r = requests.get(login_IP, timeout=1)
        # r.encoding ='utf-8'  # 设置编码格式
        req = r.url
    except:
        req = 'False'

    if signed_in_title in req:
        ToastNotifier().show_toast(title="该设备已经登录",
                                   msg="校园网状态",
                                   icon_path=already_icon,
                                   duration=5,
                                   threaded=False)
        os._exit(0)

    elif not_sign_in_title in req:
        r = requests.get(sign_parameter, timeout=1)
        r.encoding = 'utf-8'
        req = r.text
        if result_return in req:
            ToastNotifier().show_toast(title="登录成功",
                                       msg="校园网状态",
                                       icon_path=success_icon,
                                       duration=5,
                                       threaded=False)
        else:
            ToastNotifier().show_toast(title="登录失败",
                                       msg="校园网状态",
                                       icon_path=false_icon,
                                       duration=5,
                                       threaded=False)

        os._exit(0)

    else:
        ToastNotifier().show_toast(title="未连接到校园网",
                                   msg="校园网状态",
                                   icon_path=unknown_icon,
                                   duration=5,
                                   threaded=False)
        os._exit(0)
